package com.tencent.sr.iris.activity.dependency.operate;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.tencent.sr.iris.activity.common.config.QuFaConfig;
import com.tencent.sr.iris.activity.common.util.QuFaHttpUtil;
import com.tencent.sr.iris.activity.dependency.dto.common.GroupTaskBoardUtils;
import com.tencent.sr.iris.activity.dependency.dto.common.OperateBoardUtils;
import com.tencent.sr.iris.activity.dependency.dto.group.AdvisorBoardCommissionSecondDTO;
import com.tencent.sr.iris.activity.dependency.dto.group.AdvisorBoardFirstOrderSecondDTO;
import com.tencent.sr.iris.activity.dependency.dto.group.AdvisorBoardInviteSecondDTO;
import com.tencent.sr.iris.activity.dependency.dto.group.AdvisorBoardSprintSecondDTO;
import com.tencent.sr.iris.activity.dependency.dto.group.GroupTaskBaseDTO;
import com.tencent.sr.iris.activity.interfaces.request.operate.OperateBoardRequest;
import com.tencent.sr.iris.activity.interfaces.response.group.operate.OperateBoardCommissionResponse;
import com.tencent.sr.iris.activity.interfaces.response.group.operate.OperateBoardFirstOrderResponse;
import com.tencent.sr.iris.activity.interfaces.response.group.operate.OperateBoardInviteResponse;
import com.tencent.sr.iris.activity.interfaces.response.group.operate.OperateBoardSprintResponse;
import com.tencent.sr.rmall.common.request.PaginationRequest;
import com.tencent.sr.rmall.common.response.PaginationResponse;
import com.tencent.sr.rmall.springbootstarter.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

@Component
@Slf4j
public class OperateBoardDependency {

    @Resource
    private QuFaHttpUtil quFaHttpUtil;
    @Resource
    private QuFaConfig config;

    /**
     * 运营平台--冲刺赛
     *
     * @param req
     * @param url
     * @return
     */
    public PaginationResponse<OperateBoardSprintResponse> queryOperateSprint(PaginationRequest<OperateBoardRequest> req,
            String url) {
        PaginationResponse<OperateBoardSprintResponse> response = new PaginationResponse<>();
        JSONObject jsonObject = GroupTaskBoardUtils.toJsonString(req);
        String reqBody = jsonObject.toString();
        String result = quFaHttpUtil.postCommon(config, url, reqBody).toString();
        if (Objects.nonNull(result)) {
            ObjectNode jsonNodes = JsonUtils.parseObject(result);
            JsonNode data = jsonNodes.get("data");
            GroupTaskBaseDTO<AdvisorBoardSprintSecondDTO> groupTaskBaseDTO = JSONObject.parseObject(data.toString(),
                    new TypeReference<GroupTaskBaseDTO<AdvisorBoardSprintSecondDTO>>() {
                    });
            if (Objects.nonNull(groupTaskBaseDTO) && CollectionUtils.isNotEmpty(groupTaskBaseDTO.getData())) {
                List<AdvisorBoardSprintSecondDTO> data1 = groupTaskBaseDTO.getData();
                List<OperateBoardSprintResponse> list = data1.stream()
                        .map(dto -> OperateBoardUtils.toOperateBoardSprintResponse(dto))
                        .collect(Collectors.toList());
                response.setDataList(list);
                response.setPageNum(groupTaskBaseDTO.getCurrentPage());
                response.setPageSize(groupTaskBaseDTO.getPerPage());
                response.setTotalCount(groupTaskBaseDTO.getTotal());
            }
            return response;
        }
        return response;
    }

    /**
     * 运营平台--邀新有礼
     *
     * @param req
     * @param url
     * @return
     */
    public PaginationResponse<OperateBoardInviteResponse> queryOperateInvite(
            PaginationRequest<OperateBoardRequest> req, String url) {
        PaginationResponse<OperateBoardInviteResponse> response = new PaginationResponse<>();
        JSONObject jsonObject = GroupTaskBoardUtils.toJsonString(req);
        String reqBody = jsonObject.toString();
        String result = quFaHttpUtil.postCommon(config, url, reqBody).toString();
        if (Objects.nonNull(result)) {
            ObjectNode jsonNodes = JsonUtils.parseObject(result);
            JsonNode data = jsonNodes.get("data");
            GroupTaskBaseDTO<AdvisorBoardInviteSecondDTO> groupTaskBaseDTO = JSONObject.parseObject(data.toString(),
                    new TypeReference<GroupTaskBaseDTO<AdvisorBoardInviteSecondDTO>>() {
                    });
            if (Objects.nonNull(groupTaskBaseDTO) && CollectionUtils.isNotEmpty(groupTaskBaseDTO.getData())) {
                List<AdvisorBoardInviteSecondDTO> data1 = groupTaskBaseDTO.getData();
                List<OperateBoardInviteResponse> list = data1.stream()
                        .map(dto -> OperateBoardUtils.toOperateBoardInviteResponse(dto))
                        .collect(Collectors.toList());
                response.setDataList(list);
                response.setPageNum(groupTaskBaseDTO.getCurrentPage());
                response.setPageSize(groupTaskBaseDTO.getPerPage());
                response.setTotalCount(groupTaskBaseDTO.getTotal());
            }
            return response;
        }
        return response;
    }

    /**
     * 运营平台--佣金回滚
     *
     * @param request
     * @param url
     * @return
     */
    public PaginationResponse<OperateBoardCommissionResponse> queryOperateCommission(
            PaginationRequest<OperateBoardRequest> request, String url) {
        PaginationResponse<OperateBoardCommissionResponse> response = new PaginationResponse<>();
        response.setDataList(Collections.emptyList());
        response.setTotalCount(0);
        response.setPageSize(request.getPageSize());
        response.setPageNum(request.getPageNum());

        JSONObject jsonObject = GroupTaskBoardUtils.toJsonString(request);
        String reqBody = jsonObject.toString();
        String result = quFaHttpUtil.postCommon(config, url, reqBody).toString();
        if (Objects.nonNull(result)) {
            ObjectNode jsonNodes = JsonUtils.parseObject(result);
            JsonNode data = jsonNodes.get("data");
            GroupTaskBaseDTO<AdvisorBoardCommissionSecondDTO> groupTaskBaseDTO = JSONObject.parseObject(data.toString(),
                    new TypeReference<GroupTaskBaseDTO<AdvisorBoardCommissionSecondDTO>>() {
                    });
            if (Objects.nonNull(groupTaskBaseDTO) && CollectionUtils.isNotEmpty(groupTaskBaseDTO.getData())) {
                List<AdvisorBoardCommissionSecondDTO> data1 = groupTaskBaseDTO.getData();
                List<OperateBoardCommissionResponse> list = data1.stream()
                        .map(dto -> OperateBoardUtils.toOperateBoardCommissionResponse(dto))
                        .collect(Collectors.toList());
                response.setDataList(list);
                response.setPageNum(groupTaskBaseDTO.getCurrentPage());
                response.setPageSize(groupTaskBaseDTO.getPerPage());
                response.setTotalCount(groupTaskBaseDTO.getTotal());
            }
            return response;
        }
        return response;
    }

    /**
     * 运营平台 - 粉丝首单
     *
     * @param request
     * @param url
     * @return
     */
    public PaginationResponse<OperateBoardFirstOrderResponse> queryOperateFirstOrder(
            PaginationRequest<OperateBoardRequest> request, String url) {
        PaginationResponse<OperateBoardFirstOrderResponse> response = new PaginationResponse<>();
        response.setDataList(Collections.emptyList());
        response.setTotalCount(0);
        response.setPageSize(request.getPageSize());
        response.setPageNum(request.getPageNum());

        JSONObject jsonObject = GroupTaskBoardUtils.toJsonString(request);
        String reqBody = jsonObject.toString();
        String result = quFaHttpUtil.postCommon(config, url, reqBody).toString();
        if (Objects.nonNull(result)) {
            ObjectNode jsonNodes = JsonUtils.parseObject(result);
            JsonNode data = jsonNodes.get("data");
            GroupTaskBaseDTO<AdvisorBoardFirstOrderSecondDTO> groupTaskBaseDTO = JSONObject.parseObject(data.toString(),
                    new TypeReference<GroupTaskBaseDTO<AdvisorBoardFirstOrderSecondDTO>>() {
                    });
            if (Objects.nonNull(groupTaskBaseDTO) && CollectionUtils.isNotEmpty(groupTaskBaseDTO.getData())) {
                List<AdvisorBoardFirstOrderSecondDTO> data1 = groupTaskBaseDTO.getData();
                List<OperateBoardFirstOrderResponse> list = data1.stream()
                        .map(OperateBoardUtils::toOperateFirstOrderSecondResponse)
                        .collect(Collectors.toList());
                response.setDataList(list);
                response.setPageNum(groupTaskBaseDTO.getCurrentPage());
                response.setPageSize(groupTaskBaseDTO.getPerPage());
                response.setTotalCount(groupTaskBaseDTO.getTotal());
            }
            return response;
        }
        return response;
    }

    /**
     * 分页查询区发信息
     *
     * @param request
     * @return
     */
    public <Req, Res, Page extends PaginationResponse<Res>, Dto> Page queryPageFromQufa(
            PaginationRequest<Req> request, String url, Function<PaginationRequest<Req>, String> reqFunc,
            Function<Dto, Res> resFunc, Class<Dto> dtoClass, Class<Page> pageClass) {
        Page response = null;

        String reqBody = reqFunc.apply(request);
        String result = quFaHttpUtil.postCommon(config, url, reqBody).toString();
        if (Objects.nonNull(result)) {
            ObjectNode jsonNodes = JsonUtils.parseObject(result);
            JsonNode data = jsonNodes.get("data");
            GroupTaskBaseDTO<Dto> groupTaskBaseDTO = JSONObject.parseObject(data.toString(),
                    new TypeReference<GroupTaskBaseDTO<Dto>>(dtoClass) {
                    });
            response = JSONObject.parseObject(data.toString(), pageClass);
            response.setDataList(Collections.emptyList());
            response.setTotalCount(0);
            response.setPageSize(request.getPageSize());
            response.setPageNum(request.getPageNum());

            if (Objects.nonNull(groupTaskBaseDTO) && CollectionUtils.isNotEmpty(groupTaskBaseDTO.getData())) {
                List<Dto> data1 = groupTaskBaseDTO.getData();
                List<Res> list = data1.stream()
                        .map(dto -> resFunc.apply(dto)).collect(Collectors.toList());
                response.setDataList(list);
                response.setPageNum(groupTaskBaseDTO.getCurrentPage());
                response.setPageSize(groupTaskBaseDTO.getPerPage());
                response.setTotalCount(groupTaskBaseDTO.getTotal());
            }
            return response;
        }
        return response;
    }

    /**
     * 查询区发信息，返回对象
     *
     * @param request
     * @return
     */
    public <Req, Res, Dto> Res queryObjectFromQufa(Req request, String url,
            Function<Req, String> reqFunc, Function<Dto, Res> resFunc, Class<Dto> dtoClass) {

        String reqBody = reqFunc.apply(request);
        String result = quFaHttpUtil.postCommon(config, url, reqBody).toString();
        if (Objects.nonNull(result)) {
            ObjectNode jsonNodes = JsonUtils.parseObject(result);
            JsonNode data = jsonNodes.get("data");
            Dto dto = JSON.parseObject(data.toString(), dtoClass);
            return resFunc.apply(dto);
        }
        return null;
    }

    /**
     * 分页查询区发信息
     *
     * @param request
     * @return
     */
    public <Req, Res, Dto> List<Res> queryListFromQufa(Req request, String url,
            Function<Req, String> reqFunc, Function<Dto, Res> resFunc, Class<Dto> dtoClass) {

        String reqBody = reqFunc.apply(request);
        String result = quFaHttpUtil.postCommon(config, url, reqBody).toString();
        if (Objects.nonNull(result)) {
            ObjectNode jsonNodes = JsonUtils.parseObject(result);
            JsonNode data = jsonNodes.get("data");
            List<Dto> dtoList = JSONObject.parseObject(data.toString(), new TypeReference<List<Dto>>(dtoClass) {
            });
            if (CollectionUtils.isNotEmpty(dtoList)) {
                return dtoList.stream().map(dto -> resFunc.apply(dto))
                        .collect(Collectors.toList());
            }
        }
        return Collections.emptyList();
    }
}
